package com.dykj.fireFighting.common.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

public class DateBetweenUtil {

    public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            int year = min.get(Calendar.YEAR);
            int month = min.get(Calendar.MONTH);
            LocalDate firstDayOfCurrentDate = LocalDate.of(year, month + 1, 1);
            LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());
            result.add(lastDayOfCurrentDate.toString());
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    /**
     * 获取时间段内所有季度
     *
     * @param minDate 开始时间
     * @param maxDate 结束时间
     */
    public static List<String> getSeasonList(String minDate, String maxDate) throws ParseException {

        Set<String> resultSet = new HashSet<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            int year = min.get(Calendar.YEAR);
            int month = min.get(Calendar.MONTH);
            LocalDate firstDayOfCurrentDate = LocalDate.of(year, month + 1, 1);
            //LocalDate lastDayOfCurrentDate = firstDayOfCurrentDate.with(TemporalAdjusters.lastDayOfMonth());
            int m = (month + 1 + 2) / 3;
            if (m == 2) {
                String season = String.valueOf(year) + "-" + UUIDUtils.int2str(4);
                resultSet.add(season);
            } else if (m == 3) {
                String season = String.valueOf(year) + "-" + UUIDUtils.int2str(7);
                resultSet.add(season);
            } else if (m == 4) {
                String season = String.valueOf(year) + "-" + UUIDUtils.int2str(10);
                resultSet.add(season);
            } else {
                String season = String.valueOf(year) + "-" + UUIDUtils.int2str(1);
                resultSet.add(season);
            }
            curr.add(Calendar.MONTH, 1);
        }
        // set中是倒序, 重新排序
        return resultSet.stream().sorted().collect(Collectors.toList());
    }

    /**
     * 获取时间段内所有年
     *
     * @param minDate 开始时间
     * @param maxDate 结束时间
     */
    public static List<String> getYearBetween(String minDate, String maxDate) throws ParseException {

        Set<String> resultSet = new HashSet<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            int year = min.get(Calendar.YEAR);
            String season = String.valueOf(year) + "-01-01";
            resultSet.add(season);
            curr.add(Calendar.MONTH, 1);
        }
        // set中是倒序, 重新排序
        return resultSet.stream().sorted().collect(Collectors.toList());
    }

}
